﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SoccerEvaluator.Common;

namespace SoccerEvaluator.Business
{
    /// <summary>
    /// Efectua una actualizacion de marcadores de la fifa
    /// </summary>
    public class ScoreUpdater
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="projectpath"></param>
        /// <param name="url"></param>
        /// <param name="xPathTable"></param>
        /// <param name="startDate"></param>
        /// <param name="endDate"></param>
        public static void UpdateProject(int projectId, string url, string xPathTable, DateTime startDate, DateTime endDate,TemplateInfo result)
        {   
            

            string urlPage = "";

            //Si ya esta en modo de juego, permite la actualizacion
            if (result.EnableUpdates)
            {
                DateTime actualDate = startDate;

                TimeSpan ts;

                do
                {
                    ts = endDate - actualDate;

                    urlPage = string.Format("{0}date={1:00}{2}.html", url, actualDate.Month, actualDate.Year);

                    //Resultados que proporciona el proveedor
                    FIFA.FifaProvider provider = new FIFA.FifaProvider(urlPage, xPathTable);

                    List<TemplateScoreInfo> FIFAscores = provider.GetScores();

                    foreach (TemplateScoreInfo s in FIFAscores)
                    {
                        //Busca el partido en la lista
                        TemplateScoreInfo finded = result.Scores.Find(delegate(TemplateScoreInfo f) { return f.Location.ToLower() == s.Location.ToLower() && f.Local.ToLower() == s.Local.ToLower() && f.Visitor.ToLower() == s.Visitor.ToLower(); });

                        if (finded != null)
                        {
                            finded.LocalGoals = s.LocalGoals;
                            finded.VisitorGoals = s.VisitorGoals;
                        }
                    }


                    actualDate = actualDate.AddMonths(1);


                } while (ts.TotalDays > 0);

                
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="providerPath"></param>
        /// <param name="date"></param>
        /// <returns></returns>
        public static List<TemplateScoreInfo> GetScores(string providerPath, DateTime date)
        {
            /*
            Template loader = new Template();

            ProviderInfo provider = loader.LoadProvider(providerPath);

            string url = string.Format("{0}date={1:00}{2}.html", provider.UrlScores, date.Month, date.Year);

            //Resultados que proporciona el proveedor
            FIFA.FifaProvider fifa = new FIFA.FifaProvider(url, provider.TableResults);

            return fifa.GetScores();
             */

            return null;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public static string[] ListProviders(string path)
        {
            List<string> names = new List<string>();

            string[] files = System.IO.Directory.GetFiles(path);

            foreach (var i in files)
            {
                if (i.EndsWith(".xml"))
                {
                    System.IO.FileInfo f = new System.IO.FileInfo(i);

                    names.Add(f.Name.Substring(0, f.Name.Length - 4));
                }
            }

            return names.ToArray();

        }
    }
}
